﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using VCN.App.DataTransferObjects;
using VCN.App.DataAccessLayer;

namespace VCN.admin.UC
{
    public partial class uploadSildeUc : System.Web.UI.UserControl
    {
        #region Fields

        public enum eForm
        {
            insert=1,
            update=2
        }
        eForm m_eform=eForm.insert;
        ImageSlideDAO m_img_dao;
        ImageSlide m_obj_img;

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            int v_i_id;
            if (!IsPostBack)
            {                
                if (Request.QueryString["DId"] == null)
                {
                    lblTitle.Text = "THÊM ẢNH MỚI";                    
                }
                else
                {
                    lblTitle.Text = "CHỈNH SỬA HÌNH ẢNH";
                    m_bnt_submit.Text = "Update";                    
                    if (int.TryParse(Request.QueryString["DId"].ToString(), out v_i_id))
                    {
                        loadObj2Form(v_i_id);
                        ucupload.FileName = m_obj_img.Image;
                    }
                    else
                        Response.Redirect("");

                }
            }
        }
        protected void define_e_form()
        {
            if (Request.QueryString["DId"] == null)
            {
                m_eform = eForm.insert;
            }
            else m_eform = eForm.update;
        }
        protected void bnt_upload_click(object sender, EventArgs e)
        {
            try
            {                
                if (m_eform == eForm.insert)
                {
                    m_img_dao = new ImageSlideDAO();
                    form2Object();
                    m_img_dao.Insert(m_obj_img);
                    //m_lbl_Status.Text = "Your image has been uploaded!";
                    Response.Redirect("../admin/slideManage.aspx");
                }
                else
                {
                    m_img_dao = new ImageSlideDAO();                    
                    form2Object();
                    m_img_dao.update(m_obj_img);
                    //m_lbl_Status.Text = "Your image has been updated!";
                    Response.Redirect("../admin/slideManage.aspx");
                }
            }
            catch (Exception v_ex)
            {                
            }
        }                
        // Gán giá trị từ form vào đối tượng
        public void form2Object()
        {
            //Them cac dinh dang file

            ucupload.ContentFileType = "png, jpg";
            ucupload.EnableRename = false;
            ucupload.SaveToDirectory = "../home/Images/SlideShow/";

            m_obj_img = new ImageSlide();
            // gan vao doi tuong vi
            if (Request.QueryString["DId"]!=null)
                m_obj_img.Id = int.Parse(Request.QueryString["DId"].ToString());
            m_obj_img.Order = int.Parse(drdNo.Text.ToString());
            if (ucupload.HasFile == false)
            {                
                m_obj_img.Image = lblChose.Text;               
            }
            else
                m_obj_img.Image = ucupload.StartUpload();

        }

        private void loadObj2Form(int ip_Id)
        {
            m_img_dao = new ImageSlideDAO();
            m_obj_img = ImageSlideDAO.Convert(m_img_dao.Select(ip_Id).Rows[0]);
            drdNo.Text = m_obj_img.Order.ToString();
            lblChose.Text = m_obj_img.Image;
        }
        
    }
}